import { Message } from 'element-ui'

export const exportFileFn = function (exportFn, fileName, params) {
    return new Promise((resolve, reject) => {
        Message({
            type: "success",
            message: "正在导出，请稍后",
        });
        let xmlResquest = new XMLHttpRequest();
        xmlResquest.open("POST", exportFn, true);
        xmlResquest.setRequestHeader("Content-type", "application/json");
        xmlResquest.setRequestHeader(
            "Authorization",
            sessionStorage.getItem("token"),
        );
        xmlResquest.responseType = "blob";

        xmlResquest.onload = function (oEvent) {
            if (xmlResquest.status != 200) {
                reject(xmlResquest)
            } else {
                let content = xmlResquest.response;
                let elink = document.createElement("a");
                elink.download = `${fileName}.xlsx`;
                elink.style.display = "none";
                let blob = new Blob([content]);
                elink.href = URL.createObjectURL(blob);
                document.body.appendChild(elink);
                elink.click();
                document.body.removeChild(elink);
                resolve(xmlResquest)
            }
        };
        // let params = this.axiosParams()
        xmlResquest.send(
            JSON.stringify(params)
        );
    })
}